<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="资产分类" prop="cate_id">
          <treeselect v-model="queryParams.cate_id" :options="cateOptions" style="width:180px"
          :normalizer="normalizer" placeholder="请选择资产分类" />
      </el-form-item>
      <el-form-item label="仓库" prop="house_id">        
        <el-select v-model="queryParams.house_id" placeholder="请选择仓库" clearable>
          <el-option
            v-for="u in houseOptions"
            :key="u.value"
            :label="u.label"
            :value="u.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用人" prop="user_id">
        <el-select v-model="queryParams.user_id" placeholder="请选择使用人" filterable clearable>
          <el-option
            v-for="u in userOptions"
            :key="u.value"
            :label="u.label"
            :value="u.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资产编码" prop="num">
        <el-input
          v-model="queryParams.num"
          placeholder="请输入资产编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择盘点状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.assets_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['asset:detail:import']"
          >导入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['asset:detail:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="detailList"
    >
      <el-table-column label="资产分类" align="center" prop="cate_name" />
      <el-table-column label="资产型号" align="center" prop="model_name" />
      <el-table-column label="资产编码" align="center" prop="num" />
      <el-table-column label="归属仓库" prop="house_id" width="150">
        <template slot-scope="scope">
          <el-select v-model="scope.row.house_id" placeholder="请选择" clearable>
            <el-option
                v-for="u in houseOptions"
                :key="u.value"
                :label="u.label"
                :value="u.value"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="盘点状态" prop="status" width="150">
        <template slot-scope="scope">
          <el-select v-model="scope.row.status" placeholder="请选择" @change="stockAsset(scope.row)">
            <el-option
              v-for="dict in dict.type.assets_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="使用人" prop="user_id" width="150">
        <template slot-scope="scope">
          <el-select v-model="scope.row.user_id" placeholder="请选择" @change="stockAsset(scope.row)" filterable clearable>
          <el-option
            v-for="u in userOptions"
            :key="u.value"
            :label="u.label"
            :value="u.value"
          />
        </el-select>
        </template>
      </el-table-column>
      <el-table-column label="原价值" prop="price1" width="100" />
      <el-table-column label="当前价值" prop="price" width="200">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.price" @change="stockAsset(scope.row)" style="width:150px;"/>
        </template>
      </el-table-column>
      <el-table-column label="盘点时间" align="center" prop="stock_time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stock_time) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pager.page"
      :limit.sync="pager.limit"
      @pagination="getList"
    />
    <import-dialog
      :open="upload"
      model="asset_stock_detail"
      @cancel="importCancel"
      @success="importSuccess"
    />
  </div>
</template>

<script>
import {
  apiGet,
  apiPost,
  apiPut,
  apiDelete,
  apiDs,
  apiExport,
  apiDownload,
} from "@/api/openapi";
import ImportDialog from "@/components/ImportDialog";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Detail",
  components: {
    ImportDialog, Treeselect
  },
  dicts: ["assets_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 盘点明细表格数据
      detailList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      stockId: null,
      // 查询参数
      queryParams: {
        stock_id: null,
        asset_id: null,
        house_id: null,
        status: null,
        price: null,
        cate_name: null,
        model_name: null,
        num: null,
        "@alias": "t",
        "@exp": {
          // 资产编码模糊搜索
          num: "lk",
        },
      },
      pager: {
        page: 1,
        limit: 10,
        order: "id desc",
      },
      // 导入参数
      upload: false,
      // 表单参数
      form: {},
      houseOptions: [],
      userOptions: [],
      cateOptions: [],
      modelOptions: [],
      // 表单校验
      rules: {
        stock_id: [
          { required: true, message: "盘点主键不能为空", trigger: "blur" },
        ],
        asset_id: [
          { required: true, message: "资产ID不能为空", trigger: "blur" },
        ],
        cate_name: [
          { required: true, message: "资产分类不能为空", trigger: "blur" },
        ],
        model_name: [
          { required: true, message: "资产型号不能为空", trigger: "blur" },
        ],
        num: [{ required: true, message: "资产编码不能为空", trigger: "blur" }],
      },
    };
  },
  created() {      
    const stockId = this.$route.params && this.$route.params.stockId;
    this.stockId = stockId;

    this.getList();
    apiDs('asset_house_select').then(response=>{
        this.houseOptions = response.data;
    });
    apiDs('sys_user_select').then(response=>{
        this.userOptions = response.data;
    });
    apiDs('asset_model_select').then(response=>{
        this.modelOptions = response.data;
    });
    apiDs('asset_model_tree').then(response=>{
        this.cateOptions = this.handleTree(response.data, "value", "pid");
    });
  },
  methods: {
    /** 查询盘点明细列表 */
    getList() {
      this.loading = true;
      let params = {};
      this.queryParams.stock_id = this.stockId;
      params["asset_stock_detail"] = this.queryParams;
      params["@pager"] = this.pager;
      apiGet(params).then((response) => {
        this.detailList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 转换资产分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.value,
        label: node.label,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      let data = {};
      data["asset_stock_detail"] = { id: id };
      apiGet(data).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改盘点明细";
      });
    },
    //更新一个资产信息
    stockAsset(data){
        let params = {};
        params["asset_stock_detail"] =  {...data};
        if(data.user_id == null || data.user_id == '' || data.user_id == 0){
            params["asset_stock_detail"]['user_id'] = "@null";
        }
        if(data.status == null || data.status == '' || data.status == 0){
            params["asset_stock_detail"]['status'] = "@null";
        }
        params["@tags"] = ["asset_stock_detail"];
        apiPut(params).then((response) => {
            this.$modal.msgSuccess("修改成功");
        });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload = true;
    },
    importSuccess(msg) {
      this.upload = false;
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    importCancel() {
      this.upload = false;
    },
    /** 导出按钮操作 */
    handleExport() {
      let params = {};
      params["asset_stock_detail"] = this.queryParams;
      params["@pager"] = { page: 1, limit: 10000 };
      apiExport(params).then((response) => {
        apiDownload(response.msg);
      });
    },
  },
};
</script>
